package com.travel.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.travel.dao.HotelFavoriteDao;
import com.travel.entity.Comment;
import com.travel.entity.Hotel;
import com.travel.entity.HotelFavorite;
import com.travel.entity.ScenicFavorite;
import com.travel.entity.User;
import com.travel.util.DBUtils;
import com.travel.util.PageInfo;

/**
* @Title HotelFavoriteDaoImpl.java
* @Package com.travel.dao.impl
* @Author zengbochao
* @Time 2020年10月29日 上午9:13:54
* @Version 1.0
* @Description: TODO
*/
public class HotelFavoriteDaoImpl implements HotelFavoriteDao {
	
	private DBUtils db=new DBUtils();

	@Override
	public PageInfo<HotelFavorite> findByPage(int pageNum, int pageSize, int userId) {
		List<HotelFavorite> data=new ArrayList<HotelFavorite>();
		StringBuffer sb=new StringBuffer();
		sb.append("select")
			.append(" F.favorite_id,")
			.append(" F.user_id,")
			.append(" F.hotel_id,")
			.append(" U.user_name,")
			.append(" H.hotel_name,")
			.append(" H.hotel_picture")
			.append(" FROM tab_hotel_favorite F")
			.append(" JOIN tab_user U")
			.append(" ON U.user_id=F.user_id")
			.append(" JOIN tab_hotel H")
			.append(" ON F.hotel_id=H.hotel_id")
			.append(" where U.user_id=?")
			.append(" limit ?,?");
		ResultSet rs=db.query(sb.toString(), userId,(pageNum-1)*pageSize, pageSize);
		try {
			while (rs.next()) {
				Integer favoriteId=rs.getInt("favorite_id");
				userId=rs.getInt("user_id");
				Integer hotelId=rs.getInt("hotel_id");
				String  userName = rs.getString("user_name");
				String  hotelName = rs.getString("hotel_name");
				String	hotePicture=rs.getString("hotel_picture");
				HotelFavorite hotelFavorite=new HotelFavorite(favoriteId,userId,hotelId,new User(null,userName,null,null,null,null,null,null),new Hotel(null,hotelName,null,hotePicture,null,null,null));
				data.add(hotelFavorite);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 2.获取总记录数(未分页之前)
			int total = 0;
			sb = new StringBuffer();
			sb.append("select")
			  .append(" count(*)")
			  .append(" from tab_hotel_favorite ")
			  .append(" where user_id like concat('%',?,'%')");
			
			rs  = db.query(sb.toString(),userId);
			// 遍历结果集
			try {
				while (rs.next()) {
					total  = rs.getInt(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// 构建分页对象
			PageInfo<HotelFavorite> pageInfo = new PageInfo<HotelFavorite>(data, pageNum, pageSize, total);
			
			return pageInfo;
	}

	@Override
	public int insert(HotelFavorite hotelFavorite) {
		// TODO Auto-generated method stub
		return db.update("INSERT into tab_hotel_favorite(favorite_id,user_id,hotel_id) VALUES(NULL,?,?)", hotelFavorite.getUserId(),hotelFavorite.getHotelId());
	}

	@Override
	public int deleteById(int favoriteId) {
		// TODO Auto-generated method stub
		return db.update("delete from tab_hotel_favorite where favorite_id=?",favoriteId);
	}

	
	@Override
	public List<HotelFavorite> queryFavoriteListByUserIdAndHotelId(int userId,int hotelId) {

		String sql = "select favorite_id,user_id,hotel_id from tab_hotel_favorite where user_id = ? and hotel_id = ?";
		
		ResultSet rs = db.query(sql, userId, hotelId);
		List<HotelFavorite> list = new ArrayList<HotelFavorite>();
		try {
			while (rs.next()) {
				int favoriteId = rs.getInt("favorite_id");
				int userId2 = rs.getInt("user_id");
				int hotelId2 = rs.getInt("hotel_id");
				
				HotelFavorite hotelFavorite = new HotelFavorite(favoriteId, userId2, hotelId2, null, null);
				list.add(hotelFavorite);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return list;
	}
}








